package cn.oversky.eom;

import cn.oversky.eom.model.ComplexDemoPojo;
import cn.oversky.eom.model.SimpleDemoPojo;
import cn.oversky.eom.model.ThirdPartyConstant;
import cn.oversky.eom.rule.FormValueRule;
import com.alibaba.fastjson.JSON;
import org.apache.poi.ss.usermodel.Row;
import org.junit.Test;

import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static cn.oversky.eom.utils.PropertiesUtils.isNotNull;

/**
 * Created by ye on 2019/3/12.
 */
public class Demo {

    private String mapping = "{\"name\":{\"columnName\":\"姓名\",\"fieldName\":\"name\",\"thRequired\":true},\"sex\":{\"columnName\":\"性别\",\"fieldName\":\"sex\",\"thRequired\":true},\"birthday\":{\"columnName\":\"生日\",\"fieldName\":\"birthday\",\"thRequired\":true}}";

    private String thConvertConfig = "{ \"baseInfo\": \"cn.oversky.eom.model.SimpleDemoPojo\",  \"work\": \"cn.oversky.eom.model.WorkInfo\"  }";

    @Test
    public void testExcelToList() throws IOException {
        String excelPath = "demo.xlsx";
        InputStream in = getInputStream( excelPath );
        // 获取表的数据
        ExcelUtil eu = new ExcelUtil(excelPath, in);
        // 排除表头数据
        eu.setStartReadPos(0);
        List<Row> rows = eu.readExcel();
        List<SimpleDemoPojo> tList = new ArrayList<SimpleDemoPojo>();
        ExcelRowToObjectUtil.fillInList(tList, rows, mapping, SimpleDemoPojo.class);
        System.out.println(JSON.toJSONString(tList));
    }

    @Test
    public void testComplexExcelToList() throws IOException {
        String excelPath = "demoComplex.xlsx";
        InputStream in = getInputStream( excelPath );
        // 获取表的数据
        ExcelUtil eu = new ExcelUtil( excelPath, in);
        // 排除表头数据
        eu.setStartReadPos(0);
        List<Row> rows = eu.readExcel();
        List<SimpleDemoPojo> tList = new ArrayList<SimpleDemoPojo>();
        ThDotConvert thConvert = new ThDotConvert(thConvertConfig);
        thConvert.setThRows( 1 );
        ExcelRowToObjectUtil.fillInList(tList, rows, getMap(), ComplexDemoPojo.class, 0, thConvert);
        System.out.println(JSON.toJSONString(tList));
    }


    @Test
    public void textListToExcel() throws NoSuchFieldException, IOException {
        byte[] bytes = ExcelRowToObjectUtil.fillInBytes(getData(), mapping);

        String home = this.getClass().getClassLoader().getResource("").getPath();
        String fileName = home + "export.xlsx";
        System.out.println( fileName );
        FileOutputStream out = new FileOutputStream( fileName );
        out.write(bytes);
        out.flush();
        out.close();
    }

    @Test
    public void testRegexp() {
        Pattern compile = Pattern.compile(ThirdPartyConstant.GLOBAL_PHONE_REGEXP);
        Matcher matcher = compile.matcher("15678954457");
        System.out.println( matcher.matches() );
    }

    private List<SimpleDemoPojo> getData() {
        List<SimpleDemoPojo> simpleDemoPojos = new ArrayList<SimpleDemoPojo>();
        SimpleDemoPojo simpleDemoPojo = new SimpleDemoPojo();
        simpleDemoPojo.setSex( "男" );
        simpleDemoPojo.setName("测试导出，列的宽度自适应" );
        simpleDemoPojos.add(simpleDemoPojo);
        return simpleDemoPojos;
    }


    public InputStream getInputStream( String filename) throws FileNotFoundException {
        String fileName = this.getClass().getClassLoader().getResource(filename).getPath();
        FileInputStream in = new FileInputStream(fileName);
        return in;
    };


    public Map<String, EOMapping> getMap() {
        Map<String, EOMapping> map = new LinkedHashMap<String, EOMapping>();
        map.put("name", new EOMapping("baseInfo.name", "个人基本信息.姓名", null, null, ValueRuleEnum.resumeNameNotNullRule.getRule()) );
        map.put("sex", new EOMapping("baseInfo.sex", "个人基本信息.性别", getSetDict()));
        map.put("birthday",
                new EOMapping("baseInfo.birthday", "个人基本信息.生日")
                        .setRule(ValueRuleEnum.birthdayRule.getRule()));
        map.put("mobile", new EOMapping("baseInfo.mobile", "个人基本信息.移动电话", null, null, ValueRuleEnum.mobileRule.getRule()));
        map.put("companyName", new EOMapping("work.companyName", "工作.公司"));

        return map;
    }

    public Map getSetDict() {
        return new HashMap<String, String>() {{
               put("男", "1");
               put( "女","2");
        }};
    }



}
